package com.biz.hunter.dbutil;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

import org.logicalcobwebs.proxool.ProxoolException;
import org.logicalcobwebs.proxool.configuration.JAXPConfigurator;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * 
 * @author 
 * 
 */
public class ConnectionPool {
	private static final Logger logger = LoggerFactory.getLogger(ConnectionPool.class);
	private static String connName = "proxool.jdbc";

	/**
	 * 初始化连接池
	 * 
	 * @param file
	 *            proxool连接池配置文件
	 * @param jdbcName
	 *            连接池名
	 * @throws ProxoolException
	 * @throws Exception
	 */
	public static void configure(String file, String jdbcName) throws ProxoolException, Exception {
		logger.info("DB config file: "+file);
		connName = jdbcName;
		JAXPConfigurator.configure(file, false);
	}

	/**
	 * 获取数据库连�?
	 * 
	 * @return
	 */
	public static Connection getConnection() {
		Connection conn = null;
		try {
			 conn = DriverManager.getConnection(connName);
		} catch (SQLException e) {
			logger.error("", e);
		} catch (Exception e) {
			logger.error("", e);
		}
		return conn;
	}
	

	/**
	 * 关闭连接
	 * 
	 * @param conn
	 * @throws SQLException
	 */
	public static void closeConn(Connection conn) {
		if (conn != null) {
			try {
				conn.close();
			} catch (SQLException e) {
				logger.error("", e);
			}
		}
	}
}
